package com.utils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @ClassName http
 * @Description TODO
 * @Author milkywayeye
 * @Date 2023/11/6 10:03
 * @Version V1.0
 **/
@Service
public class Http {

    public String httpPost(String url, MultiValueMap<String, Object> data) {
        RestTemplate restTemplate = new RestTemplate();
//        return restTemplate.postForEntity(url, name, String.class).getBody();
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = "";
//        try {
//            json = objectMapper.writeValueAsString(data);
//// 打印JSON字符串
//
//        } catch (JsonProcessingException e) {
//
//            e.printStackTrace();
//
//        }
//        // 配置基础请求参数
//        String requestBody = json;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 组装发送postForEntity请求
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(data, headers);
        System.out.println(param);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, param, String.class);
        String body = postForEntity.getBody();
        return body;
    }

    //发送视频获取回调视频地址请求
    public String httpVideo(String url, MultiValueMap<String, Object> data) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 组装发送postForEntity请求
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(data, headers);
        System.out.println(param);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, param, String.class);
        String body = postForEntity.getBody();
        return body;
    }
}
